import { message, Spin, Form, Input, Modal } from "antd";
import { useState, useEffect } from "react";
import api_opt from "@/api/system/option";
const { TextArea } = Input;
const Edit = (props) => {
    const { close, callback, _id } = props;
    const [ifShowModel, setifShowModel] = useState(true);
    const [loading, setLoading] = useState(false);
    const [form] = Form.useForm();
    useEffect(() => {
        if (_id !== -1) getModel();
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, []);
    const initialValues = { type: "", name: "", remark: "" };
    //点击确认
    const onFinish = async () => {
        let valid;
        await form.validateFields().catch(() => {
            valid = true;
        });
        if (valid) return;
        console.log("_id", _id);
        if (_id === -1) add();
        else update();
    };
    //获取实体
    const getModel = async () => {
        setLoading(true);
        try {
            let res = await api_opt.getoption({ _id });
            form.setFieldsValue(res.data[0]);
            setLoading(false);
        } catch (err) {
            setLoading(false);
        }
    };
    //新增
    const add = async () => {
        try {
            setLoading(true);
            let res = await api_opt.addoption({ ...form.getFieldsValue(true), operateType: "root" });
            setLoading(false);
            message.success(res.msg);
            setifShowModel(false);
            callback();
        } catch (err) {
            console.log("err", err);
            setLoading(false);
        }
    };
    //更新
    const update = async () => {
        try {
            setLoading(true);
            let res = await api_opt.updateoption({ ...form.getFieldsValue(true), operateType: "root" });
            setLoading(false);
            message.success(res.msg);
            setifShowModel(false);
            callback();
        } catch (err) {
            setLoading(false);
        }
    };
    return (
        <>
            <Modal
                title={_id === -1 ? "添加字典" : "编辑字典"}
                open={ifShowModel}
                onOk={onFinish}
                onCancel={() => {
                    setifShowModel(false);
                }}
                afterClose={() => {
                    form.resetFields();
                    close();
                }}
                width={600}>
                <Spin spinning={loading} delay={500}>
                    <Form form={form} labelCol={{ span: 4 }} initialValues={initialValues} name="basic" autoComplete="off">
                        <Form.Item label="字典名称：" name="name" rules={[{ required: true, message: "请输入字典名称!" }]}>
                            <Input placeholder="请输入字典名称" />
                        </Form.Item>

                        <Form.Item label="字典类型：" name="type" rules={[{ required: true, message: "请输入字典类型!" }]}>
                            <Input placeholder="请输入字典类型" />
                        </Form.Item>
                        <Form.Item label="备注：" name="remark">
                            <TextArea placeholder="请输入字典备注" />
                        </Form.Item>
                    </Form>
                </Spin>
            </Modal>
        </>
    );
};

export default Edit;
